package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLocationServiceImp implements UserLocationService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override    // 上传地理位置
    public void uploadLocation(Long userId, Double longitude, Double latitude, String addrStr) {
        //创建条件为userId相同
        Query query = new Query(Criteria.where("userId").is(userId));
        //调用mongo查询满足条件用户位置
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //如果为空 创建用户位置类赋值保存
        if (userLocation == null) {
            userLocation = new UserLocation();
            userLocation.setAddress(addrStr);
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());
            userLocation.setLocation(new GeoJsonPoint(longitude, latitude));
            userLocation.setUserId(userId);
            mongoTemplate.save(userLocation);
        } else {//如果不为空 修改用户位置已及修改时间并保持
            userLocation.setLocation(new GeoJsonPoint(longitude, latitude));
            userLocation.setLastUpdated(userLocation.getUpdated());
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setAddress(addrStr);
            mongoTemplate.save(userLocation);
        }
    }

    @Override//查询附件的人
    public List<Long> searchNearUserId(Long userId, Double distance) {
        //调用mongo查询出条件为userId相同的用户位置的经纬度
        GeoJsonPoint point = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class).getLocation();
        //创建半径范围类 赋值
        Distance dis = new Distance(distance / 1000, Metrics.KILOMETERS);
        // 以中心点画个指定半径的圆
        Circle circle = new Circle(point, dis);
        //在圆范围中查找
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        //查询出满足条件的用户位置列表
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);
        //创建满足条件的用户位置的用户id集合
        List<Long> longs = new ArrayList<>();
        //判断满足条件的用户位置列表是否非空 遍历
        if (CollectionUtil.isNotEmpty(userLocations)) {
            for (UserLocation userLocation : userLocations) {
                //把满足条件的用户位置的用户id添加到集合
                longs.add(userLocation.getUserId());
            }
        }
        //返回用户id集合
        return longs;
    }
}
